<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\order;

use app\controller\api\ApiController;
use app\exception\LogicException;
use app\model\communal\OrderConsultModel;
use app\service\logic\common\order\OrderConsultService;
use app\validate\common\order\ConsultValidate;
use app\validate\common\order\MultiValidate;
use think\App;
use think\response\Json;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class ConsultController extends ApiController
{
    protected $service;

    public function __construct(App $app, OrderConsultService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ConsultValidate::class . '.index');
        $user = $this->request->middleware('user');
        $search['user_id'] = $user['id'];
        $search['group_id'] = 0;

        $with = ['belongUser', 'belongExpert'];
        $fun = function ($item) {
            list($item['status_map'], $item['status_sub']) = $this->service->getStatusMap($item['status'], $item);
            $item['purchase_time_map'] = $item['purchase_time'] / 60; // s换算min
            $item['consult_method'] = 1 == $item['consult_method'] ? '语音咨询' : '视频咨询';
            $item['payment_type_map'] = $this->service->getPaymentTypeMap($item['payment_type']);
            unset($item['belongUser'], $item['belongExpert']);
            return $item;
        };
        $result = $this->service->getList($params, $search_fields, $search, $with, $fun);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 立即支付(单个订单, 古怪订单组忽略...)
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws LogicException
     * @throws ModelNotFoundException
     */
    public function repay()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.repay');
        $user = $this->request->middleware('user');
        $params['user_id'] = $user['id'];

        $result = $this->service->repay($params);
        return amis($result);
    }

    /**
     * 咨询进行中
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws LogicException
     * @throws ModelNotFoundException
     */
    public function going()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = $this->service->going($params);
        return amis($result);
    }

    /**
     * 取消订单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws LogicException
     */
    public function cancel()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = $this->service->cancel($params);
        return amis($result);
    }

    /**
     * 变更为待填写报告
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function report()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = $this->service->report($params);
        return amis($result);
    }

    /**
     * 订单发起退款
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws LogicException
     * @throws ModelNotFoundException
     */
    public function refund()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');
        $user = $this->request->middleware('user');
        $params['operator'] = $user['name'];

        $result = $this->service->refund($params);
        return amis($result);
    }

    /**
     * 订单取消退款
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws LogicException
     * @throws ModelNotFoundException
     */
    public function unrefund()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = $this->service->unrefund($params);
        return amis($result);
    }

    /**
     * 显示资源
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $with = ['belongUser', 'belongExpert'];
        $fun = function ($item) {
            list($item['status_map'], $item['status_sub']) = $this->service->getStatusMap($item['status'], $item);
            $item['purchase_time_map'] = $item['purchase_time'] / 60; // s换算min
            $item['consult_method_map'] = 1 == $item['consult_method'] ? '语音咨询' : '视频咨询';
            $item['payment_type_map'] = $this->service->getPaymentTypeMap($item['payment_type']);
            unset($item['belongUser'], $item['belongExpert']);
            return $item;
        };
        $result = $this->service->read($params, $with, $fun);
        return amis($result);
    }

    /**
     * 筛选 订单类别
     *
     * @return Json
     */
    public function type()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$typeOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * 筛选 订单状态
     *
     * @return Json
     */
    public function status()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$statusOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * 排序选项
     *
     * @return Json
     */
    public function order()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$orderOptions);
        return amis(['options' => $options]);
    }
}
